<template>
  <div class="on-site-highlights">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">现场集锦</h1>
        <p class="page-subtitle">展会现场的精彩瞬间和亮点回顾</p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('exhibition-news')"
            :class="['nav-item', { active: activeTab === 'exhibition-news' }]"
          >
            展会资讯
          </a>
          <a
            @click="setActiveTab('media-reports')"
            :class="['nav-item', { active: activeTab === 'media-reports' }]"
          >
            媒体报道
          </a>
          <a
            @click="setActiveTab('industry-news')"
            :class="['nav-item', { active: activeTab === 'industry-news' }]"
          >
            行业资讯
          </a>
          <a
            @click="setActiveTab('on-site-highlights')"
            :class="[
              'nav-item',
              { active: activeTab === 'on-site-highlights' },
            ]"
          >
            现场集锦
          </a>
          <a
            @click="setActiveTab('cooperative-media')"
            :class="['nav-item', { active: activeTab === 'cooperative-media' }]"
          >
            合作媒体
          </a>
          <a
            @click="setActiveTab('media-registration')"
            :class="[
              'nav-item',
              { active: activeTab === 'media-registration' },
            ]"
          >
            媒体注册
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="content-section">
        <!-- <h2 class="section-title">现场集锦</h2>
        <p v-if="pageData.description" class="section-desc">
          {{ pageData.description }}
        </p> -->

        <!-- 多条数据展示：图片在上，文本在下 -->
        <div class="highlights-grid">
          <div
            class="highlight-item"
            v-for="highlight in highlightsData"
            :key="highlight.id"
          >
            <!-- 图片在上 -->
            <div class="highlight-image-section" v-if="highlight.image">
              <img
                :src="highlight.image"
                :alt="highlight.title"
                class="highlight-image"
              />
            </div>
            <!-- 文本在下 -->
            <div class="highlight-content">
              <h3 class="highlight-title">{{ highlight.title }}</h3>
              <div
                v-html="highlight.content"
                class="highlight-content-text"
              ></div>
              <div class="highlight-meta">
                <span class="highlight-date"></span>
                <span
                  class="highlight-type"
                  @click="navLisks(highlight?.linkUrl)"
                  >{{ highlight.type }}</span
                >
              </div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { subpageApi } from "@/api/user";
import { ElMessage } from "element-plus";

export default {
  name: "OnSiteHighlights",
  setup() {
    const activeTab = ref("on-site-highlights");
    // 页面基本信息
    const pageData = ref({});
    const bannerImage = ref("");

    // 现场集锦数据
    const highlightsData = ref([]);

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content) return { content: "", image: "", date: "", type: "" };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return { content: content, image: "", date: "", type: "" };
      } catch (e) {
        return { content: content, image: "", date: "", type: "" };
      }
    };

    // 加载现场集锦数据
    const loadHighlights = async () => {
      try {
        const response = await subpageApi.getSubpageList(
          "media",
          "on-site-highlights"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 取第一条作为页面基本信息
          const firstItem = response.data[0];
          pageData.value = {
            title: firstItem.title || "现场集锦",
            description:
              firstItem.description ||
              (firstItem.content &&
              !(firstItem.content || "").trim().startsWith("[") &&
              !(firstItem.content || "").trim().startsWith("{")
                ? firstItem.content
                : ""),
          };

          // 处理多条现场集锦数据
          highlightsData.value = response.data.map((item, index) => {
            const parsed = parseContent(item.content);
            return {
              id: item.id || index + 1,
              title: item.title || "",
              content: parsed.content || item.description || "",
              image: item.imageUrl || parsed.image || "",
              date: parsed.date || new Date().toLocaleDateString(),
              type: parsed.type || "现场集锦",
              linkUrl: item.linkUrl || parsed.linkUrl || "",
            };
          });
        } else {
          // 使用默认数据
          pageData.value = getDefaultPageData();
          highlightsData.value = getDefaultHighlights();
        }
      } catch (error) {
        console.error("加载现场集锦数据失败:", error);
        pageData.value = getDefaultPageData();
        highlightsData.value = getDefaultHighlights();
      }
    };

    const loadBanner = async () => {
      try {
        const res = await subpageApi.getContentByType("carousel_media");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };

    // 跳转链接
    const navLisks = (link) => {
      if (!link) return ElMessage.warning("当前集锦未设置跳转链接！");
      console.log(link);

      window.open(link, "_blank");
    };

    onMounted(() => {
      loadHighlights();
      loadBanner();
    });

    // 默认页面数据
    const getDefaultPageData = () => {
      return {
        title: "现场集锦",
        description: "展会现场的精彩瞬间和亮点回顾",
      };
    };

    // 默认现场集锦数据
    const getDefaultHighlights = () => {
      return [
        {
          id: 1,
          title: "开幕式精彩瞬间",
          content:
            "<p>FAIR plus 2025展会开幕式在热烈的氛围中拉开帷幕。</p><p>来自全球的机器人企业代表和行业专家齐聚一堂，共同见证这一重要时刻。</p>",
          image:
            "https://images.unsplash.com/photo-1581091226825-a6a2a5aee158?w=400&h=300&fit=crop",
          date: "2024-12-15",
          type: "开幕式",
        },
        {
          id: 2,
          title: "机器人演示现场",
          content:
            "<p>展会现场各种机器人演示吸引了众多观众驻足观看。</p><p>从工业机器人到服务机器人，展示了机器人技术的多样性和实用性。</p>",
          image:
            "https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=400&h=300&fit=crop",
          date: "2024-12-14",
          type: "产品演示",
        },
        {
          id: 3,
          title: "技术交流论坛",
          content:
            "<p>同期举办的技术交流论坛为行业专家提供了深入交流的平台。</p><p>与会者就机器人技术发展趋势和产业应用进行了热烈讨论。</p>",
          image:
            "https://images.unsplash.com/photo-1565043589221-1a6fd9a6d0b8?w=400&h=300&fit=crop",
          date: "2024-12-13",
          type: "论坛活动",
        },
        {
          id: 4,
          title: "观众互动体验",
          content:
            "<p>观众可以近距离体验各种机器人产品，感受科技的魅力。</p><p>互动体验区成为展会的一大亮点，深受观众喜爱。</p>",
          image:
            "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=300&fit=crop",
          date: "2024-12-12",
          type: "互动体验",
        },
      ];
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "on-site-highlights") {
        window.location.href = `/${tabKey}`;
      }
    };

    return {
      activeTab,
      bannerImage,
      pageData,
      highlightsData,
      setActiveTab,
      navLisks,
    };
  },
};
</script>

<style scoped>
.on-site-highlights {
  min-height: 100vh;
  background: #f8f9fa;
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.content-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
  position: relative;
  padding-left: 15px;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 24px;
  background: linear-gradient(
    135deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
  border-radius: 2px;
}

.section-desc {
  margin: -8px 0 2rem;
  color: #666;
  text-align: center;
  font-size: 1.1rem;
  line-height: 1.6;
}

/* 多条数据网格布局 */
.highlights-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  /* margin-top: 2rem; */
}

.highlight-item {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.highlight-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

/* 图片在上 */
.highlight-image-section {
  width: 100%;
  position: relative;
  padding-bottom: 60%;
  /* height: 200px; */
  overflow: hidden;
}

.highlight-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background-color: #f8f9fa;
}

/* 文本在下 */
.highlight-content {
  padding: 1.5rem;
}

.highlight-title {
  font-size: 1.3rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 1rem;
  text-align: center;
}

.highlight-content-text {
  color: #666;
  line-height: 1.6;
  text-align: left;
  margin-bottom: 1rem;
}

/* 富文本内图片限制 */
.highlight-content-text :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 8px;
}

.highlight-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 1rem;
  border-top: 1px solid #eee;
  font-size: 0.9rem;
  color: #999;
}

.highlight-date {
  font-weight: 500;
}

.highlight-type {
  background: var(--e-global-color-selected);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.8rem;
  cursor: pointer;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .content-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .highlights-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
